package com.yitongmao.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.activity.AmendActivity;
import com.yitongmao.bean.AddressListBean;
import com.yitongmao.bean.Addressmoren;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

/*
* 作者：梁伟航 on 2017/7/28 12:02
* 类的用途：
*/
public class AddressListAdapter extends BaseAdapter{
    private Context context;
    private boolean flag=true;
    private LoginHuancunBean loginHuancunBean;
    private List<AddressListBean.AddressBean> address;
    private  String userId;
    private int loginId1;

    public AddressListAdapter(Context context, List<AddressListBean.AddressBean> address) {
        this.context=context;
        this.address=address;
        Gson gson=new Gson();
        String loginId = ACache.get(context).getAsString("loginId");
        if (loginId!=null){
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            int id = loginHuancunBean.userInfo.id;
            userId = String.valueOf(id);
        }
    }
    @Override
    public int getCount() {
        return address.size();
    }

    @Override
    public Object getItem(int position) {
        return address.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.addresslistitem, null);
            viewHolder.mAddressName = (TextView) convertView.findViewById(R.id.mAddressName);
            viewHolder.mAddressphone = (TextView) convertView.findViewById(R.id.mAddressphone);
            viewHolder.mAddressbody = (TextView) convertView.findViewById(R.id.mAddressbody);
            viewHolder.mMorendizhi=(AutoLinearLayout)convertView.findViewById(R.id.mMorendizhi);
            viewHolder.mBianji=(AutoLinearLayout)convertView.findViewById(R.id.mBianji);
            viewHolder.mShanchu=(AutoLinearLayout)convertView.findViewById(R.id.mShanchu);
            viewHolder.mAddressmoren = (ImageView) convertView.findViewById(R.id.mAddressmoren);
            convertView.setTag(viewHolder);//绑定ViewHolder对象
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mAddressName.setText(address.get(position).name);
        viewHolder.mAddressphone.setText(address.get(position).phone);
        viewHolder.mAddressbody.setText(address.get(position).provinceName+""+address.get(position).cityName+""+address.get(position).countryName+""+address.get(position).address);
        if (address.get(position).status.equals("01")){
            viewHolder.mAddressmoren.setImageResource(R.drawable.dizhixuanzhong);
            notifyDataSetChanged();
        }else {
            viewHolder.mAddressmoren.setImageResource(R.drawable.dizhimeixuanzhong);
            notifyDataSetChanged();
        }
        //点击事件
       viewHolder.mMorendizhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag){
                    viewHolder.mAddressmoren.setImageResource(R.drawable.dizhixuanzhong);
                    flag=true;
                    int id = address.get(position).id;
                    String s = String.valueOf(id);
                    loginId1 = loginHuancunBean.userInfo.loginId;
                    userDefaultAmendUserMessage(userId,s);
                    notifyDataSetChanged();
                }
            }
        });
        //删除
        viewHolder.mShanchu.setOnClickListener(new View.OnClickListener() {
            @Override//删除
            public void onClick(View v) {
                int cityId = address.get(position).id;
                userSexAmendUserMessage(String.valueOf(cityId));
                address.remove(position);
                notifyDataSetChanged();
            }
        });
        //编辑
        viewHolder.mBianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String provinceName = address.get(position).provinceName;
                String cityName = address.get(position).cityName;
                String countryName = address.get(position).countryName;
                String name = address.get(position).name;
                String phone = address.get(position).phone;
                int id = address.get(position).id;
                String status = address.get(position).status;
                String address = AddressListAdapter.this.address.get(position).address;
                Intent intent=new Intent(context, AmendActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("phone", phone);
                intent.putExtra("provinceName", provinceName);
                intent.putExtra("cityName", cityName);
                intent.putExtra("countryName", countryName);
                intent.putExtra("address", address);
                intent.putExtra("id", String.valueOf(id));
                intent.putExtra("status", status);
                context.startActivity(intent);
                // 转化为activity，然后finish就行了
                Activity activity = (Activity)context;
                activity.finish();
            }
        });
        return convertView;
    }

    public void userSexAmendUserMessage(String userId){
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("id",userId);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin",SignUtil.signLogin(nonce,timestamp));
        params.put("loginId",String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.deleteAddressUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                Log.e("zzz","..."+result);
            }
            @Override
            public void onFail(Object result) {
            }
        });
    }
    public void userDefaultAmendUserMessage(final String userId, String id){
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("userId",userId);
        params.put("id",id);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin",SignUtil.signLogin(nonce,timestamp));
        params.put("loginId",String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.addressDefault, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                Log.e("zzz","..."+result);
                String string = (String) result;
                Gson gson = new Gson();
                Addressmoren addressmoren = gson.fromJson(string, Addressmoren.class);
                if (addressmoren.result.state.equals("1")){
                    addressList(userId);
                }
                ToastUtils.showShort(context,""+addressmoren.result.message);
            }
            @Override
            public void onFail(Object result) {
            }
        });
    }

    public void addressList(String userId) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("userId", userId);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin",SignUtil.signLogin(nonce,timestamp));
        params.put("loginId",String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.addressListUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Log.e("Gson", "" + string);
                Gson gson = new Gson();
                AddressListBean addressListBean = gson.fromJson(string, AddressListBean.class);
                List<AddressListBean.AddressBean> address1 = addressListBean.address;
                if (addressListBean.result.state.equals("1")) {
                    address.clear();
                    address=address1;
                    notifyDataSetChanged();
                }
            }
            @Override
            public void onFail(Object result) {
                ToastUtils.showShort(context, "失败");
            }
        });

    }
    static class ViewHolder {
        public TextView mAddressName;
        public TextView mAddressphone;
        public TextView mAddressbody;
        public AutoLinearLayout mMorendizhi;
        public AutoLinearLayout mBianji;
        public AutoLinearLayout mShanchu;
        public ImageView mAddressmoren;
    }
}
